<template>
  <div class="app-container">
    <el-card style="width: 100%">
      <table-list
        ref="tableList"
        :fields="tableDataColumn"
        :pagination="true"
        :searchFunc="getUserProductList"
        :searchDefault="searchConditions"
        border></table-list>
    </el-card>
  </div>
</template>
<script>
import { getUserProductList, getList as getUserList } from '@/api/user';
import { getList as getProductList } from '@/api/product';
import TableList from '@/components/TableList/index';
import CryptoJS from 'crypto-js';
export default {
  name: 'list',
  components: {
    TableList,
  },
  data() {
    return {
      getUserProductList,
      tableDataColumn: [
        {
          prop: 'id',
          searchName: 'id',
          label: 'ID',
          minWidth: '60',
          searchType: 'input',
          exact: true,
        },
        {
          prop: 'product_key',
          searchName: 'product_key',
          label: '序列号',
          minWidth: '80',
          searchType: 'input',
          exact: true,
        },
        {
          prop: 'product.product_name',
          searchName: 'product_id',
          label: '产品名称',
          minWidth: '180',
          searchType: 'remoteSelect',
          searchAction: getProductList,
          searchKey: 'id',
          searchLabel: 'product_name',
          exact: true,
        },
        {
          prop: 'product.product_no',
          searchName: 'product_id',
          label: '产品编号',
          minWidth: '180',
          searchType: 'remoteSelect',
          searchAction: getProductList,
          searchKey: 'id',
          searchLabel: 'product_no',
          exact: true,
        },
        {
          prop: 'unit_price',
          searchName: 'unit_price',
          label: '单价',
          minWidth: '100',
          searchType: 'input',
          exact: true,
        },
        {
          prop: 'request_number',
          searchName: 'request_number',
          label: '请求数量',
          minWidth: '100',
        },
        {
          prop: 'charge_number',
          searchName: 'charge_number',
          label: '扣费数量',
          minWidth: '100',
        },
        {
          prop: 'remark',
          searchName: 'remark',
          label: '备注',
          minWidth: '160',
          searchType: 'input',
        },
        {
          prop: 'created_at',
          searchName: 'created_at',
          label: '创建时间',
          minWidth: '180',
          searchType: 'dateTime',
        },
        {
          label: '操作',
          searchField: false,
          render: (h, scope) => {
            return (
              <div>
                <el-button
                  size='mini'
                  type='text'
                  onClick={() => {
                    this.gotoMenu(scope.row);
                  }}>
                  立即使用
                </el-button>
                <el-button
                  size='mini'
                  type='text'
                  onClick={() => {
                    this.gotoStatistics(scope.row);
                  }}>
                  查看统计
                </el-button>
              </div>
            );
          },
          fixed: 'right',
          minWidth: '160',
        },
      ],
    };
  },
  computed: {
    searchConditions() {
      return {
        user_id: this.$store.state.user.info.id,
      };
    },
  },
  methods: {
    gotoMenu(row) { 
      let componentName = row.product.component.split('/');
      componentName.shift();
      this.$router.push({
        path: componentName.join('/'),
      });
    },
    async gotoStatistics(row){
      let productListData = await getProductList({ where: { category_id:row.product.category_id } })
      if( productListData.data && productListData.data.total && productListData.data.total > 0 ){
        let produtIds = JSON.stringify(productListData.data.data.map(el=>el.id))
        var str = CryptoJS.enc.Utf8.parse(produtIds);
        var res = CryptoJS.enc.Base64.stringify(str);
        let routeUrl = this.$router.resolve({
          path: '/statistics/record',
          query: { key: res, type: CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('customer')), categoryName:row.product.category.category_name },
        });
        window.open(routeUrl.href, '_blank');
      } else {
        this.$message({
          message: '当前分类无产品',
          type: 'warning'
        });
      }
    }
  },
};
</script>
